<template>
  <el-dialog
    :title="state.title"
    @close="handleClose"
    width="500"
    v-model="state.visible"
    :before-close="beforeClose"
    :loading="state.loading"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
    destroy-on-close
  >
    <div style="padding: 10px">
      <el-descriptions :column="1" border class="custom-descriptions">
        <el-descriptions-item :label="t('business.ledgerSettleDealer.tranId')">{{ledgerSettleDealer.tranId}}</el-descriptions-item>
        <el-descriptions-item :label="t('business.ledgerSettleDealer.type')">
          <DictTag type="info" :options="LedgerTypeEnum" :value="ledgerSettleDealer.type" />
        </el-descriptions-item>
        <el-descriptions-item :label="t('business.ledgerSettleDealer.name')">
          <span v-if="ledgerSettleDealer.type == 'Platform'">-</span>
          <span v-if="ledgerSettleDealer.type == 'Tenant'">{{ledgerSettleDealer.tenantName}}</span>
          <span v-if="ledgerSettleDealer.type == 'Agent'">{{ledgerSettleDealer.agentName}}</span>
          <span v-if="ledgerSettleDealer.type == 'Dealer'">{{ledgerSettleDealer.dealerName}}</span>
        </el-descriptions-item>
        <el-descriptions-item :label="t('business.ledgerSettleDealer.amount')">{{ledgerSettleDealer.amount}}</el-descriptions-item>
        <el-descriptions-item :label="t('business.ledgerSettleDealer.state')"><DictTag type="info" :options="LedgerSettleStateEnum" :value="ledgerSettleDealer.state" /></el-descriptions-item>
        <el-descriptions-item :label="t('business.ledgerSettleDealer.settleTime')">{{ledgerSettleDealer.settleTime}}</el-descriptions-item>
      </el-descriptions>
      <br />
      <el-form label-width="120px" :model="form" ref="formRef">
        <el-row>
          <el-col :span="24">
            <el-input type="textarea" rows="2" v-model="form.note" :placeholder="t('crud.search.pleaseInput')" />
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
      <el-button @click="handleClose" :loading="state.loading">{{t('crud.cancelText')}}</el-button>
      <el-popconfirm :title="t('business.ledgerSettleDealer.tooltip.paidConfirm')" @confirm="handleSubmit">
        <template #reference>
          <el-button type="primary" :loading="state.loading">{{t('business.ledgerSettleDealer.action.paid')}}</el-button>
        </template>
      </el-popconfirm>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>

import {useI18n} from 'vue-i18n'
import {ledgerSettleDealerPaid} from '@/api/business/ledgerSettleDealer'
import {parseTime} from '@/utils/ruoyi'
import {ComponentInternalInstance} from 'vue'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { LedgerSettleStateEnum, LedgerTypeEnum } = toRefs<any>(proxy?.useEnum('ledger_settle_state_enum', 'ledger_type_enum'))

const { t } = useI18n()

const emits = defineEmits(['success'])

const form = ref<any>({
  id: undefined,
  note: '',
})

const state = reactive({
  visible: false,
  loading: false,
  title: t('business.ledgerSettleDealer.action.paid'),
})

const ledgerSettleDealer = ref<any>({})

const openDialog = (row?: any) => {
  ledgerSettleDealer.value = row
  form.value.id = row.id
  form.value.note = ''
  state.visible = true
}

const handleClose = () => {
  state.visible = false
}

const handleSubmit = () => {
  state.loading = true
  ledgerSettleDealerPaid(form.value).then((res)=>{
    ElMessage.success(t('operate.tooltip.success'))
    handleClose()
    emits('success', res)
  }).catch((e)=>{
    console.log(e)
  }).finally(()=>{
    state.loading = false
  })
}

const beforeClose = (done: () => void) => {
  done()
}
defineExpose({
  openDialog,
})
</script>
